const authService = require('../services/authService');

const authController = {
  // 用户注册
  async register(ctx) {
    try {
      const { username, email, password } = ctx.request.body;
      
      // 验证输入
      if (!username || !email || !password) {
        ctx.status = 400;
        ctx.body = { message: '请填写完整的注册信息' };
        return;
      }
      
      const userId = await authService.register({ username, email, password });
      
      ctx.status = 201;
      ctx.body = {
        message: '注册成功',
        userId
      };
    } catch (error) {
      ctx.status = 400;
      ctx.body = { message: error.message };
    }
  },

  // 用户登录
  async login(ctx) {
    try {
      const { email, password } = ctx.request.body;
      
      // 验证输入
      if (!email || !password) {
        ctx.status = 400;
        ctx.body = { message: '请提供邮箱和密码' };
        return;
      }
      
      const { token, user } = await authService.login({ email, password });
      
      ctx.status = 200;
      ctx.body = {
        message: '登录成功',
        token,
        user
      };
    } catch (error) {
      ctx.status = 401;
      ctx.body = { message: error.message };
    }
  },

  // 获取当前用户信息
  async getCurrentUser(ctx) {
    try {
      // 假设用户ID从认证中间件中获取
      const userId = ctx.state.user.id;
      
      const user = await authService.getCurrentUser(userId);
      
      ctx.status = 200;
      ctx.body = user;
    } catch (error) {
      ctx.status = 401;
      ctx.body = { message: '认证失败' };
    }
  },

  // 更新用户信息
  async updateUser(ctx) {
    try {
      const userId = ctx.state.user.id;
      const { username, email } = ctx.request.body;
      
      // 验证输入
      if (!username || !email) {
        ctx.status = 400;
        ctx.body = { message: '请提供完整的用户信息' };
        return;
      }
      
      await authService.updateUser(userId, { username, email });
      
      ctx.status = 200;
      ctx.body = { message: '用户信息更新成功' };
    } catch (error) {
      ctx.status = 400;
      ctx.body = { message: error.message };
    }
  },

  // 修改密码
  async changePassword(ctx) {
    try {
      const userId = ctx.state.user.id;
      const { currentPassword, newPassword } = ctx.request.body;
      
      // 验证输入
      if (!currentPassword || !newPassword) {
        ctx.status = 400;
        ctx.body = { message: '请提供当前密码和新密码' };
        return;
      }
      
      await authService.changePassword(userId, currentPassword, newPassword);
      
      ctx.status = 200;
      ctx.body = { message: '密码修改成功' };
    } catch (error) {
      ctx.status = 400;
      ctx.body = { message: error.message };
    }
  },

  // 验证令牌（用于前端检查令牌有效性）
  async verifyToken(ctx) {
    try {
      const token = ctx.headers.authorization?.split(' ')[1];
      
      if (!token) {
        ctx.status = 401;
        ctx.body = { message: '未提供令牌' };
        return;
      }
      
      const decoded = await authService.verifyToken(token);
      
      ctx.status = 200;
      ctx.body = {
        valid: true,
        user: { id: decoded.id, email: decoded.email }
      };
    } catch (error) {
      ctx.status = 401;
      ctx.body = {
        valid: false,
        message: error.message
      };
    }
  }
};

module.exports = authController;